<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class File extends Model
{
    use HasFactory;

    protected $fillable = ['width', 'height', 'archive_id', 'size', 'file', 'type', 'md5', 'color', 'is_cdn'];

    protected $appends = ['file_url'];

    public function archive()
    {
        return $this->belongsTo(Archive::class);
    }
    public function getFileUrlAttribute()
    {
        if ($this->is_cdn) {
            return '//zhitiantu.cdn.bcebos.com/' . $this->file;
        }
        return Str::startsWith($this->file, '/storage/') || Str::startsWith($this->file, '/random_file') ? $this->file : Storage::url($this->file);
    }
}
